{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "167772161"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 测试ipParse函数，该函数将一个IP格式的字符串转化为一个整型变量\n",
    "from mininet.util import ipParse\n",
    "ipParse(\"10.0.0.1\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入: 1234:5678:1234:7896:1234:5678:1234:7896\n",
      "输出: 12345678, 12347896, 12345678, 12347896\n",
      "\n",
      "输入: FE80::200:ff:fe00:1111\n",
      "输出: FE800000, 00000000, 020000FF, FE001111\n",
      "\n",
      "输入: ::1\n",
      "输出: 00000000, 00000000, 00000000, 00000001\n",
      "\n",
      "输入: 2001:db8::1\n",
      "输出: 20010DB8, 00000000, 00000000, 00000001\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import ipaddress\n",
    "\n",
    "def ipv6_to_four_parts(ipv6_str):\n",
    "    try:\n",
    "        addr = ipaddress.IPv6Address(ipv6_str)\n",
    "    except ipaddress.AddressValueError as e:\n",
    "        raise ValueError(f\"Invalid IPv6 address: {e}\")\n",
    "\n",
    "    # 获取完整展开的 IPv6 地址（补全所有省略的 0）\n",
    "    exploded = addr.exploded\n",
    "    groups = exploded.split(':')\n",
    "    \n",
    "    if len(groups) != 8:\n",
    "        raise ValueError(\"Unexpected format after expanding IPv6 address\")\n",
    "    \n",
    "    # 转换为大写并合并为四个部分\n",
    "    groups_upper = [g.upper() for g in groups]\n",
    "    return [\n",
    "        groups_upper[0] + groups_upper[1],\n",
    "        groups_upper[2] + groups_upper[3],\n",
    "        groups_upper[4] + groups_upper[5],\n",
    "        groups_upper[6] + groups_upper[7]\n",
    "    ]\n",
    "\n",
    "# 测试示例\n",
    "test_cases = [\n",
    "    \"1234:5678:1234:7896:1234:5678:1234:7896\",\n",
    "    \"FE80::200:ff:fe00:1111\",\n",
    "    \"::1\",\n",
    "    \"2001:db8::1\"\n",
    "]\n",
    "\n",
    "for ip in test_cases:\n",
    "    try:\n",
    "        parts = ipv6_to_four_parts(ip)\n",
    "        print(f\"输入: {ip}\")\n",
    "        print(f\"输出: {', '.join(parts)}\\n\")\n",
    "    except ValueError as e:\n",
    "        print(f\"错误: {e}\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12347887"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int(\"12347887\",10)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "p4dev-python-venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
